package WizardTD.element;

import WizardTD.App;
import WizardTD.board.ManaProcessBar;
import lombok.AllArgsConstructor;
import lombok.Data;
import processing.core.PApplet;
import processing.core.PVector;

import java.util.List;

@Data
public class Fireball extends Element{
    private Monster target;
    private float damage;

    private int count = 0;

    public Fireball(float x, float y, Monster target, float damage) {
        super(x, y);
        this.target = target;
        this.damage = damage;
    }


    @Override
    public void draw(PApplet pApplet) {
        pApplet.fill(255,0,0);
        pApplet.ellipse(this.getCoordinateX(), this.getCoordinateY(), 5,5);
        updateCoordinate(60);
    }

    private void updateCoordinate(int i) {
        PVector target = new PVector(this.getTarget().getCoordinateX() + 10, this.getTarget().getCoordinateY() + 10);
        PVector current = new PVector(this.getCoordinateX(), this.getCoordinateY());
        PVector sub = target.sub(current);
        float dist = target.dist(current);
        PVector next = current.add(sub.x / dist * i, sub.y / dist * i);
        this.setCoordinateX(next.x);
        this.setCoordinateY(next.y);
    }

    public void decideAttack(List<Monster> monsters) {
        PVector fp = new PVector(this.getCoordinateX(), this.getCoordinateY());
        for (Monster m : monsters) {
            PVector mp = new PVector(m.getCoordinateX() + 10, m.getCoordinateY() + 10);
            if (mp.sub(fp).mag() <= 10) {
                float v = m.getBloodProcessBar().getCurrentHp() - this.getDamage();
                m.getBloodProcessBar().setCurrentHp(v);
                if (m.getBloodProcessBar().getCurrentHp() <= 0) {
                    App.monsterBox.getRemoveMonsters().add(m);
                    ManaProcessBar mana = App.board.getManaProcessBar();
                    mana.upMana(m.getMana_gained_on_kill());
                    App.monsterBox.destroyMonsters.add(new DestroyMonster(m.getCoordinateX(), m.getCoordinateY(), 1));
                }
                App.removeFireballs.add(this);
                return;
            }
        }
    }
}
